package com.neusoft.neuiotms.dcom.converter;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.neusoft.bizcore.web.dto.AbstractConverter;
import com.neusoft.neuiotms.dcom.constants.DCOMConstants;
import com.neusoft.neuiotms.dcom.dto.InspectionTaskProjectItemValueDTO;
import com.neusoft.neuiotms.dcom.model.InspectionTaskProjectItemValue;
import com.neusoft.neuiotms.dcom.model.enums.InspectionItemValueStatus;
import com.neusoft.neuiotms.dcom.repository.InspectionTaskProjectItemValueRepository;

@Component
public class InspectionTaskProjectItemValueConverter
        extends AbstractConverter<InspectionTaskProjectItemValue, InspectionTaskProjectItemValueDTO> {

    @Autowired
    private InspectionTaskProjectItemValueRepository itemValueRepository;

    @Autowired
    private InspectionRuleProjectItemConverter ruleItemConverter;

    @Override
    public InspectionTaskProjectItemValue toModel(final InspectionTaskProjectItemValueDTO dto) {
        InspectionTaskProjectItemValue model;
        if (dto.isNew()) {
            model = new InspectionTaskProjectItemValue();
        } else {
            model = this.itemValueRepository.getOne(dto.getId());
        }
        model.setValue(dto.getValue());
        if (StringUtils.isEmpty(dto.getValue())) {
            model.setStatus(InspectionItemValueStatus.uninspected);
        } else {
            if (model.getRuleProjectItem() != null) {
                if (model.getRuleProjectItem().getValueType().equals(DCOMConstants.INSPECTION_ITEM_VALUE_TYPE_NUMBER)) {
                    final double earlyWarningDown = model.getRuleProjectItem().getEarlyWarningDown();
                    final double earlyWarningUpper = model.getRuleProjectItem().getEarlyWarningUpper();
                    final double alarmDown = model.getRuleProjectItem().getAlarmDown();
                    final double alarmUpper = model.getRuleProjectItem().getAlarmUpper();
                    final double value = Double.valueOf(dto.getValue());
                    if ((value <= alarmDown) || (value > alarmUpper)) {
                        model.setStatus(InspectionItemValueStatus.alarm);
                    } else if (((value > alarmDown) && (value <= earlyWarningDown))
                            || ((value > earlyWarningUpper) && (value < alarmUpper))) {
                        model.setStatus(InspectionItemValueStatus.earlyWarning);
                    } else {
                        model.setStatus(InspectionItemValueStatus.normal);
                    }
                } else if (model.getRuleProjectItem().getValueType()
                        .equals(DCOMConstants.INSPECTION_ITEM_VALUE_TYPE_STRING)) {
                    //                    final String alarm = model.getRuleProjectItem().getAlarm()
                    //                            .replace(DCOMConstants.INSPECTION_COMMA_CHINESE, DCOMConstants.INSPECTION_COMMA_ENGLISH);
                    //                    final String earlyWarning = model.getRuleProjectItem().getEarlyWarning()
                    //                            .replace(DCOMConstants.INSPECTION_COMMA_CHINESE, DCOMConstants.INSPECTION_COMMA_ENGLISH);
                    final List<String> alarmRange =
                            new ArrayList<>(Arrays.asList(model.getRuleProjectItem().getAlarm()
                                    .replace(DCOMConstants.INSPECTION_COMMA_CHINESE,
                                            DCOMConstants.INSPECTION_COMMA_ENGLISH)
                                    .split(DCOMConstants.INSPECTION_COMMA_ENGLISH)));
                    final List<String> earlyWarningRange =
                            new ArrayList<>(Arrays.asList(model.getRuleProjectItem().getEarlyWarning()
                                    .replace(DCOMConstants.INSPECTION_COMMA_CHINESE,
                                            DCOMConstants.INSPECTION_COMMA_ENGLISH)
                                    .split(DCOMConstants.INSPECTION_COMMA_ENGLISH)));
                    if (alarmRange.contains(dto.getValue())) {
                        model.setStatus(InspectionItemValueStatus.alarm);
                    } else if (earlyWarningRange.contains(dto.getValue())) {
                        model.setStatus(InspectionItemValueStatus.earlyWarning);
                    } else {
                        model.setStatus(InspectionItemValueStatus.normal);
                    }
                } else {
                    //do nothing
                }
            }
        }

        return model;
    }

    @Override
    public InspectionTaskProjectItemValueDTO toDTO(final InspectionTaskProjectItemValue model,
            final boolean forListView) {
        final InspectionTaskProjectItemValueDTO dto = new InspectionTaskProjectItemValueDTO();
        dto.setId(model.getId());
        dto.setStatus(model.getStatus().name());
        dto.setName(model.getRuleProjectItem().getName());
        dto.setUnit(model.getRuleProjectItem().getUnit());
        dto.setValue(model.getValue());
        dto.setValueType(model.getRuleProjectItem().getValueType());
        dto.setSort(model.getRuleProjectItem().getSort());
        dto.setRuleItem(this.ruleItemConverter.toDTO(model.getRuleProjectItem()));
        return dto;
    }

}
